package com.gxuwz.controller;

import com.gxuwz.model.entity.Classes;
import com.gxuwz.model.entity.Instructor;
import com.gxuwz.model.vo.Result;
import com.gxuwz.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/classes")
@CrossOrigin
public class ClassesController {

    @Autowired
    private ClassesService classesService;

    // 显示班级列表
    @GetMapping("/list")
    public Result<List<Classes>> listClasses() {
        List<Classes> classes = classesService.getAllClasses();
        return Result.succeed(classes);
    }

    // classID和className查询获取辅导员信息
    @GetMapping("/classID/{classID}")
    public Result<Classes> getClassByClassID(@PathVariable String classID) {
        Classes classes = classesService.getClassByClassID(classID);
        return classes != null ? Result.succeed(classes) : Result.error("未找到该班级");
    }
    @GetMapping("/className/{className}")
    public Result<Classes> getClassByClassName(@PathVariable String className) {
        Classes classes = classesService.getClassByClassName(className);
        return classes != null ? Result.succeed(classes) : Result.error("未找到该班级");
    }
    //添加班级页面
    @PostMapping("/add")
    public Result addClasses(@RequestBody Classes classes) {
        boolean result = classesService.addClass(classes);
        return result ? Result.succeed("班级添加成功") : Result.error("班级添加失败");
    }

    // 更新班级信息
    @PutMapping("/update")
    public Result updateClass(@RequestBody Classes classes) {
        System.out.println("classes:"+classes);
        boolean result = classesService.updateClass(classes);
        return result ? Result.succeed("班级信息更新成功") : Result.error("班级信息更新失败");
    }

    // 处理删除班级请求
    @DeleteMapping("/delete/{classID}")
    public Result deleteClass(@RequestBody String classID) {
        boolean isDeleted = classesService.deleteClassByClassID(classID);
        return isDeleted ? Result.succeed("班级删除成功") : Result.error("班级删除失败");
    }
}
